﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.queryzer2.Dao.Base;
using Com.queryzer2.Dao.Interface;
using Com.queryzer2.Domain.Entities;
using Oracle.DataAccess.Client;
using Com.queryzer2.Domain;
using System.Data;

namespace Com.queryzer2.Dao.Impl
{
    public class PraticaD01Dao : OracleDao<OracleSessionManager, ISessionModel>, IPraticaD01Dao
    {

        private string seletctAll = "select * from GEP_DATID01";
        private string selectById = "select * from GEP_DATID01 WHERE DD01_COD_PRATICA = :DD01_COD_PRATICA";
        private string delete = "delete GEP_DATID01 WHERE DD01_COD_PRATICA = :DD01_COD_PRATICA";

        private string update = "UPDATE ASM_GEP.GEP_DATID01"
                              + " SET DD01_DISATT_FUORI_ORARIO = :DD01_DISATT_FUORI_ORARIO, DD01_DT_NON_ESEGPRIMA = :DD01_DT_NON_ESEGPRIMA, DD01_presenza_cliente_finale = :DD01_presenza_cliente_finale"
                              + " WHERE (DD01_COD_PRATICA = :DD01_COD_PRATICA)";

        private string insert = "INSERT INTO GEP_DATID01"
                              + " (DD01_COD_PRATICA, DD01_DISATT_FUORI_ORARIO, DD01_DT_NON_ESEGPRIMA, DD01_presenza_cliente_finale)"
                              + " VALUES(:DD01_COD_PRATICA, :DD01_DISATT_FUORI_ORARIO, :DD01_DT_NON_ESEGPRIMA, :DD01_presenza_cliente_finale)";
                             //+ " RETURNING TES_COD_PRATICA INTO :TES_COD_PRATICA";

        public PraticaD01Dao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }
        
        private OracleParameter[] MappingParams(GEP_DATID01 r, bool isInsert)
        {
            OracleParameter[] oparams;
            oparams = new OracleParameter[4];

            oparams[0] = new OracleParameter("DD01_COD_PRATICA", OracleDbType.Decimal, r.Id_dd01_cod_pratica, ParameterDirection.Input);
            oparams[1] = new OracleParameter("DD01_DISATT_FUORI_ORARIO", OracleDbType.Varchar2, r.Dd01_Disatt_Fuori_Orario, ParameterDirection.Input);
            oparams[2] = new OracleParameter("DD01_DT_NON_ESEGPRIMA", OracleDbType.Date, r.Dd01_Dt_Non_Esegprima, ParameterDirection.Input);
            oparams[3] = new OracleParameter("DD01_presenza_cliente_finale", OracleDbType.Varchar2, r.Dd01_presenza_cliente_finale, ParameterDirection.Input);
            //if (isInsert)
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Output);
            //else
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Input);
            return oparams;
        }

        #region IDao<GEP_DATID01,decimal> Members

        public Dictionary<decimal, GEP_DATID01> GetAll()
        {
            throw new NotImplementedException();
        }

        public GEP_DATID01 GetById(decimal id)
        {
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectById, CurrentConnection);
            cmd.Parameters.Add("DD01_COD_PRATICA", id);
            GEP_DATID01 ut = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        ut = DaoOracleDbHelper.MapDatiD01(r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-D01003", e0x);
            }
            return ut;
        }

        public void Insert(GEP_DATID01 entity)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = insert;
                    ocmd.Parameters.AddRange(MappingParams(entity, false));
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                   // entity.Id_Tes_Cod_Pratica = (decimal)ocmd.Parameters["pID"].Value;
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-PR004", e0x);
                }
            }

        }

        public void Update(GEP_DATID01 entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(decimal id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (!base.Context.TransactionModeOn)
            {
                base.CurrentConnection.Close();
            }
        }

        #endregion
    }
}
